const app = getApp();

Page({
  data: {
    longitude:"",            //经度
    latitude:"",             //维度
    list:"",                 //商家列表
    index:0,                 //当前分类id
    myclass:[],              //商家分类
  },
  onLoad() {
    console.log(this.data.listaaaa);
    var that=this;
    my.httpRequest({
      url: 'https://www.zxty.me/index.php/Api/Shop/classList',
      method: 'GET',
      data: {},
      dataType: 'json',
      success: function(res) {
        var obj={name:"全部"};
        var arr=res.data.data;
        arr.unshift(obj);
        this.setData({
          myclass:arr
        })
      }.bind(this),
      fail: function(res) {
        my.alert({content: '获取分类列表失败'});
      },
      complete: function(res) {
        //my.hideLoading();
        //my.alert({content: 'complete'});
      }
    });
    my.getLocation({
      success(res) {
        console.log(res.longitude+res.latitude);
        that.setData({
          longitude: res.longitude,
          latitude: res.latitude
        });
        my.httpRequest({
          url: 'https://www.zxty.me/index.php/Api/Shop/shopList',
          method: 'GET',
          data: {
            lat:res.longitude,
            lnt:res.latitude,
            p:1
          },
          dataType: 'json',
          success: function(res) {
            var arr=res.data.data;
            var newarr=[];
            for(var i=0;i<arr.length;i++){
              var obj={
                id:i,
                latitude:arr[i].lat,
                longitude:arr[i].lnt,
                width:60,
                height:60,
                title:arr[i].shop_name,
                iconPath:"/image/icon.png",
                address:arr[i].address
              }
              newarr.push(obj);
            }
            console.log(newarr);
            that.setData({
              list:newarr
            });
          },
          fail: function(res) {
            my.alert({content: '获取商家列表失败'});
          },
          complete: function(res) {
            //my.hideLoading();
            //my.alert({content: 'complete'});
          }
        });
      },
      fail() {
        my.alert({ title: '定位失败' });
      }
    })
  },
  bindPickerChange(e) {
    var that=this;
    this.setData({
        index:e.detail.value,
    });
    if(e.detail.value==0){
      var obj1={
            lat:that.data.latitude,
            lnt:that.data.longitude,
            p:1
      }
    } else{
      var obj1={
            lat:that.data.latitude,
            lnt:that.data.longitude,
            p:1,
            class_id:e.detail.value
      }
    } 
    my.httpRequest({
          url: 'https://www.zxty.me/index.php/Api/Shop/shopList',
          method: 'GET',
          data: obj1,
          dataType: 'json',
          success: function(res) {
            var arr=res.data.data;
            var newarr=[];
            for(var i=0;i<arr.length;i++){
              var obj={
                id:i,
                latitude:arr[i].lat,
                longitude:arr[i].lnt,
                width:60,
                height:60,
                title:arr[i].shop_name,
                iconPath:"/image/icon.png",
                address:arr[i].address
              }
              newarr.push(obj);
            }
            console.log(newarr);
            that.setData({
              list:newarr
            });
          },
          fail: function(res) {
            my.alert({content: '获取商家列表失败2'});
          },
          complete: function(res) {
            //my.hideLoading();
            //my.alert({content: 'complete'});
          }
    });
  },
  onmarker(e){
    var that=this;
    var i =e.markerId;
    console.log(i);
    console.log(that.data.list[i]);
    my.openLocation({
      longitude: +that.data.list[i].longitude,
      latitude: +that.data.list[i].latitude,
      name: that.data.list[i].title,
      address: that.data.list[i].address
    });
  }
});
